package cn.edu.qzuie.artist.module.td.dal.mysql.work;

import java.util.*;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.edu.qzuie.artist.framework.mybatis.core.mapper.BaseMapperX;
import cn.edu.qzuie.artist.module.td.dal.dataobject.work.WorkDO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.workscatagory.WorksCatagoryDO;
import com.alibaba.excel.util.StringUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.edu.qzuie.artist.module.td.controller.admin.work.vo.*;
import org.springframework.data.repository.query.Param;

/**
 * 作品 Mapper
 *
 * @author 泉信管理员
 */
@Mapper
public interface WorkMapper extends BaseMapperX<WorkDO> {

    default PageResult<WorkDO> selectPage(WorkPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<WorkDO>()
                .likeIfPresent(WorkDO::getWorkName, reqVO.getWorkName())
                .eqIfPresent(WorkDO::getCatagoryId, reqVO.getCatagoryId())
                .eqIfPresent(WorkDO::getWorkType, reqVO.getWorkType())
                .eqIfPresent(WorkDO::getPublished, reqVO.getPublished())
                .eqIfPresent(WorkDO::getUserId, reqVO.getUserId())
                .betweenIfPresent(WorkDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(WorkDO::getId));
    }
    /**
     * 连表分页查询
     */
    default PageResult<WorkMultiplePagesVO> selectWorkWithCategoryName(PageParam pageParam,WorkPageReqVO reqVO) {
        // 构建查询条件
        MPJLambdaWrapper<WorkDO> wrapper = new MPJLambdaWrapper<>();

        // 查询 td_work 表的所有字段
        wrapper.selectAll(WorkDO.class)
                // 查询
                .select(WorksCatagoryDO::getCategoryName, WorksCatagoryDO::getId)
                // 左连接 td_works_catagory 表，连接条件为 tw.catagory_id = twc.id
                .leftJoin(WorksCatagoryDO.class, WorksCatagoryDO::getId, WorkDO::getCatagoryId);
        // 动态拼接查询条件
        if (StringUtils.isNotBlank(reqVO.getWorkName())) {
            wrapper.like(WorkDO::getWorkName, reqVO.getWorkName()); // 模糊匹配作品名称
        }
        if (reqVO.getCatagoryId() != null) {
            wrapper.eq(WorkDO::getCatagoryId, reqVO.getCatagoryId()); // 精确匹配分类 ID
        }
        if (reqVO.getWorkType() != null) {
            wrapper.eq(WorkDO::getWorkType, reqVO.getWorkType()); // 精确匹配作品类型
        }
        if (reqVO.getPublished() != null) {
            wrapper.eq(WorkDO::getPublished, reqVO.getPublished()); // 精确匹配发布状态
        }
        if (reqVO.getNickName() != null) {
            wrapper.like(WorkDO::getNickName, reqVO.getNickName()); // 模糊匹配作者
        }
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length == 2) {
            wrapper.between(WorkDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]); // 时间范围查询
        }

        // 默认排序：按 id 倒序
        wrapper.orderByDesc(WorkDO::getId);
        // 执行查询并返回结果
        return selectJoinPage(pageParam, WorkMultiplePagesVO.class, wrapper);
    }
    /**
     * 根据 ID 查询工作信息，并连表查询 parent_id
     */
    default WorkRespVO selectByIdWithParentId(@Param("id") Long id) {
        MPJLambdaWrapper<WorkDO> wrapper = new MPJLambdaWrapper<>();

        // 查询 td_work 表的所有字段
        wrapper.selectAll(WorkDO.class)
                // 查询 td_works_catagory 表的 parent_id 字段，并设置别名为 parentId
                .select(WorksCatagoryDO::getParentId, WorksCatagoryDO::getId)
                // 内连接 td_works_catagory 表，连接条件为 w.catagory_id = c.id
                .innerJoin(WorksCatagoryDO.class, WorksCatagoryDO::getId, WorkDO::getCatagoryId)
                // 添加条件：w.id = #{id}
                .eq(WorkDO::getId, id);

        // 执行查询并返回结果
        return selectJoinOne(WorkRespVO.class, wrapper);
    }


}